package com.gewu.teday.utils

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.os.Build
import android.util.DisplayMetrics
import android.view.*
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import com.gewu.teday.R
import com.github.mikephil.charting.utils.Utils


class GwDialog(context: Context) : Dialog(context) {

    private var tips = this
    private var okTvTexts: String? = null
    private var noTvTexts: String? = null
    var inflate: View? = null

    inner class LayoutBuilder() {

        fun create(
            layout: Int,
            isCancel: Boolean,
            greate: Int,
            shape: Int,
            widths: Int = 0,
            hights:Int = 0
        ): GwDialog {
            inflate = LayoutInflater.from(context).inflate(layout, null)
            setContentView(inflate!!)
            var width: Int = 0
            var hight: Int = 0
            if (greate == Gravity.BOTTOM) {
                width = ViewGroup.LayoutParams.MATCH_PARENT
                hight =if (widths == 0)  getScreenH(context) / 10 * 6 else hights
            } else {
                hight = ViewGroup.LayoutParams.WRAP_CONTENT
                width = if (widths == 0) getScreenW(context) / 10 * 8 else widths
            }
            setWindow(isCancel,
                width,
                hight,
                greate,
                shape)
            return tips
        }

        fun getView(): View? = inflate
    }

    private var onClicks: OnClickListener? = null

    interface OnClickListener {
        fun onLeftClick(dialog: GwDialog);
        fun onRightClick(dialog: GwDialog);
    }

    inner class TipBuilder() {

        private var titleTv: TextView? = null
        private var contentTv: TextView? = null
        private var ok: TextView? = null
        private var no: TextView? = null

        @SuppressLint("UseCompatLoadingForDrawables")
        fun create(title: String?, content: String?, isCancel: Boolean): GwDialog {
            inflate = LayoutInflater.from(context).inflate(R.layout.tip_layout, null)
            tips.setContentView(inflate!!)
            val bu = inflate!!.findViewById<LinearLayout>(R.id.bu)
            titleTv = inflate!!.findViewById<TextView>(R.id.title)
            contentTv = inflate!!.findViewById<TextView>(R.id.content)
            ok = inflate!!.findViewById<TextView>(R.id.ok)
            no = inflate!!.findViewById<TextView>(R.id.no)

            setWindow(isCancel,
                getScreenW(context) / 10 * 8,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                Gravity.CENTER,
                R.drawable.shape)

            if (title.isNullOrEmpty()) {
                titleTv?.text = "默认标题"
            } else {
                titleTv?.text = title
            }

            if (content.isNullOrEmpty()) {
                contentTv?.text = "这是内容内容内容内容内容内容内容内容内容内容"
            } else {
                contentTv?.text = content
            }

            if (okTvTexts.isNullOrEmpty() && noTvTexts.isNullOrEmpty()) {
                bu.visibility = View.GONE
            } else {

                if (okTvTexts.isNullOrEmpty()) {
                    ok?.visibility = View.GONE
                } else {
                    ok?.text = okTvTexts
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        ok?.setTextColor(context.getColor(R.color.red))
                    }
                    ok?.background = context.resources.getDrawable(R.drawable.select_shape)
                    ok?.setOnClickListener(View.OnClickListener {
                        onClicks?.onLeftClick(tips)
                    })
                }

                if (noTvTexts.isNullOrEmpty()) {
                    no?.visibility = View.GONE
                } else {
                    no?.text = noTvTexts
                    no?.background = context.resources.getDrawable(R.drawable.select_shape)
                    no?.setOnClickListener(View.OnClickListener {
                        onClicks?.onRightClick(tips)
                    })
                }
            }
            return tips
        }

        fun setOnClick(
            okTvText: String?,
            noTvText: String?,
            Click: OnClickListener?,
        ): TipBuilder {
            onClicks = Click
            okTvTexts = okTvText
            noTvTexts = noTvText
            return TipBuilder()
        }

    }

    private var params: WindowManager.LayoutParams? = null

    private fun setWindow(
        isCancel: Boolean,
        width: Int,
        height: Int,
        gravity: Int,
        background: Int,
    ) {
        tips.setCancelable(isCancel)
        params = tips.window?.attributes
        params?.width = width
        params?.height = height
        tips.window?.attributes = params
        tips.window?.setGravity(gravity)
        if (background != 0) {
            tips.window?.setBackgroundDrawableResource(background) //设置对话框的样式
        }
        if (params?.gravity == Gravity.BOTTOM) {
            tips.window?.setWindowAnimations(R.style.animation)
        }

    }

    /**
     * 获取的是屏幕
     */
    private fun getDisplayMetrics(context: Context): DisplayMetrics {
        val metrics = DisplayMetrics()
        getDispaly(context).getMetrics(metrics)
        return metrics
    }

    /**
     * 获取屏幕管理器
     */
    private fun getDispaly(context: Context): Display {
        val systemService =
            context.applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        return systemService.defaultDisplay
    }

    /**
     * 获取屏幕宽度
     */
    fun getScreenW(context: Context): Int {
        return getDisplayMetrics(context).widthPixels
    }

    /**
     * 获取屏幕高度
     * 不包含顶部状态栏和底部navigationBar的高度
     */
    fun getScreenH(context: Context): Int {
        return getDisplayMetrics(context).heightPixels
    }


    override fun show() {
        Util.setNaviBarColor(window)
        super.show()
    }


}